<template>
  <div class="dev">
    <!-- 搜索框 -->
    <div class="search">
      <el-button type="primary" icon="el-icon-orange" @click="creatQr">生成设备编号</el-button>
    </div>

    <!-- 表格数据 -->
    <div class="table">
      <el-table :data="tableData" ref="isExpand" v-loading="loading" border height="calc(100%-10rem)"
        highlight-current-row stripe style="width: 100%">
        >
        <template slot="empty">
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column align="center" prop="orderId" label="批次编号">
        </el-table-column>
        <el-table-column align="center" prop="orderCount" label="二维码数量">
        </el-table-column>
        <el-table-column align="center" prop="createUser" label="创建人">
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="out(scope.row)">导出</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination v-show="tableData.length !== 0" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[5, 10, 15, 20, 25, 30]"
        :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 生成设备编号 -->
    <div class="dialog2">
      <el-dialog title="生成设备编号" :visible.sync="dialogShow" @close="close" width="30%">
        <el-form label-position="right" :model="addForm" ref="addForm" label-width="100px" class="demo-ruleForm">
          <el-form-item required label="编号数量" prop="codeCount">
            <el-input-number v-model="addForm.codeCount" :min="1" :max="2000" label="描述文字"></el-input-number>
            <div style="font-size: 14px; color: red">
              (一次至多生成2000个编号)
            </div>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogShow = false">取 消</el-button>
          <el-button type="primary" :icon="icon" @click="addFn">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
  
<script>
import {
  getOrderApi,
  addOrApi,
  outOrderApi,
} from "../../../../api/system/order";
import { Loading } from "element-ui";
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      pageNum: 1,
      icon: "",
      pageSize: 10,
      total: null,
      userName: "",
      dialogShow: false,
      addForm: {
        codeCount: "100",
      },
      options: {
        fullscreen: true,
        text: "加载中...",
        background: "rgba(0,0,0,.5)",
      },
    };
  },
  created() {
    this.pageSize = Number(sessionStorage.getItem("pageSize")) || 10;
    this.pageNum = Number(sessionStorage.getItem("pageNum")) || 1;
    this.getOrder(this.pageNum, this.pageSize);
  },
  destroyed() {
    sessionStorage.removeItem("pageNum");
    sessionStorage.removeItem("pageSize");
  },
  methods: {
    close() {
      this.dialogShow = false;
      this.addForm = {
        codeCount: "100",
      };
    },
    // 获取用户列表
    getOrder(pageNum, pageSize) {
      this.loading = true;
      getOrderApi(pageNum, pageSize).then((res) => {
        this.total = res.data.total;
        if (res.data.rows.length == 0) {
          this.pageNum = this.pageNum - 1;
          if (this.pageNum > 0) {
            this.search();
          } else {
            this.tableData = res.data.rows;
            this.loading = false;
          }
        } else {
          this.loading = false;
          this.tableData = res.data.rows;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.getOrder(this.pageNum, val);
      sessionStorage.setItem("pageSize", val);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.getOrder(val, this.pageSize);
      sessionStorage.setItem("pageNum", val);
      this.pageNum = val;
    },
    // 打开生成设备编号
    creatQr() {
      this.dialogShow = true;
    },

    // 添加批次
    addFn() {
      this.icon = "el-icon-loading";
      if (!this.addForm.codeCount) {
        this.$message.error("请填写编号数量");
        this.icon = "";
      } else {
        addOrApi(this.addForm)
          .then((res) => {
            this.icon = "";
            if (res.code == 200) {
              this.$message.success("添加成功！");
              this.dialogShow = false;
              this.getOrder(this.pageNum, this.pageSize);
              this.addForm = {
                codeCount: "100",
              };
            }
          })
          .catch((err) => {
            this.icon = "";
            this.$message.error("添加失败！");
          });
      }
    },
    // 导出
    async out(row) {
      Loading.service(this.options);
      await outOrderApi(row.orderId)
        .then((res) => {
          Loading.service(this.options).close();
          if (
            res.type ==
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          ) {
            let blob = new Blob([res], { type: "application/octet-stream" });
            const link = document.createElement("a");
            link.style.display = "none";
            link.href = URL.createObjectURL(blob);
            link.setAttribute("download", "导出.xlsx");
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
          }
        })
        .catch((err) => {
          Loading.service(this.options).close();
          console.log(err);
        });
    },
  },
};
</script>
  
<style lang="scss" scoped>
.dev {
  padding-top: 15px;
  padding: 0 15px;

  .search {
    margin-bottom: 15px;

    .el-button {
      margin-left: 15px;
    }
  }

  .table {
    .el-pagination {
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>